package com.xindaoapp.happypet.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.rockerhieu.emojicon.parse.ParseMsgUtil;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.Message;
import com.xindaoapp.happypet.bean.PostListEntity.ForumInfo;
import com.xindaoapp.happypet.ui.PostDetailActivity;
import com.xindaoapp.happypet.utils.BitmapUtil;
import com.xindaoapp.happypet.utils.CommonParameter;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.view.CircleImageView;

public class MyCommunityAdapter extends MyBaseAdapter<Message> {
    private final Context mContext;
    private final List<Message> mList;
    private final int photoSize = 120;
    private ForumInfo foruminfo;

    public MyCommunityAdapter(Context context, List<Message> mList, int resId) {
        super(context, mList, resId);
        this.mContext = context;
        this.mList = mList;
    }

    public void addData(List<Message> mList) {
        this.mList.addAll(mList);
        notifyDataSetChanged();
    }

    public void clearData(List<Message> mList) {
        this.mList.clear();
        // notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View contentView, ViewGroup parent, Message t) {
        ViewHolder holder = (ViewHolder) contentView.getTag();
        final Message post = mList.get(position);

        if (null == holder) {
            holder = new ViewHolder();
            holder.ll = (LinearLayout) contentView.findViewById(R.id.ll_pet_layout);

            holder.icon1 = (ImageView) contentView.findViewById(R.id.pet_icon1);
            holder.icon2 = (ImageView) contentView.findViewById(R.id.pet_icon2);
            holder.icon3 = (ImageView) contentView.findViewById(R.id.pet_icon3);

            holder.usericon = (CircleImageView) contentView.findViewById(R.id.user_icon);
            holder.post_icon = (CircleImageView) contentView.findViewById(R.id.post_icon);
            holder.username = (TextView) contentView.findViewById(R.id.user_name);
            holder.title = (TextView) contentView.findViewById(R.id.tv_title);
            holder.type = (ImageView) contentView.findViewById(R.id.iv_top_right);
            holder.look = (TextView) contentView.findViewById(R.id.look_time);
            // holder.care = (TextView) contentView.findViewById(R.id.care_time);
            holder.message = (TextView) contentView.findViewById(R.id.message_time);
            holder.time = (TextView) contentView.findViewById(R.id.time);
            contentView.setTag(holder);
        }


        // holder.icon1.setVisibility(View.GONE);
        // holder.icon2.setVisibility(View.GONE);
        // holder.icon3.setVisibility(View.GONE);

        holder.title.setVisibility(View.VISIBLE);

        // android.view.ViewGroup.LayoutParams lp2 = iv.getLayoutParams();
        // iv.setScaleType(ScaleType.CENTER_CROP);
        // lp2.width = (CommonParameter.sScreenWidth - CommonUtil.dp2px(mContext, 70)) / 3;
        // lp2.height = (CommonParameter.sScreenWidth - CommonUtil.dp2px(mContext, 70)) / 3;
        // iv.setLayoutParams(lp2);

        // int l1 = CommonParameter.sScreenWidth - CommonUtil.dp2px(mContext, 70) / 1;
        // int l2 = CommonParameter.sScreenWidth - CommonUtil.dp2px(mContext, 70) / 2;


        // android.view.ViewGroup.LayoutParams pa1 = holder.icon1.getLayoutParams();
        // pa1.height = l3;
        // pa1.width = l3;
        // holder.icon1.setLayoutParams(pa1);
        //
        // android.view.ViewGroup.LayoutParams pa2 = holder.icon2.getLayoutParams();
        // pa2.height = l3;
        // pa2.width = l3;
        // holder.icon2.setLayoutParams(pa2);
        //
        // android.view.ViewGroup.LayoutParams pa3 = holder.icon3.getLayoutParams();
        // pa3.height = l3;
        // pa3.width = l3;
        // holder.icon3.setLayoutParams(pa3);


        int commonLp = (CommonParameter.sScreenWidth - CommonUtil.dp2px(mContext, 70)) / 3;

        for (int i = 0; i < post.getPics().size(); i++) {



            if (i == 0) {
                holder.icon1.setVisibility(View.VISIBLE);
                android.view.ViewGroup.LayoutParams lp = holder.icon1.getLayoutParams();
                holder.icon1.setScaleType(ScaleType.CENTER_CROP);
                lp.width = commonLp;
                lp.height = commonLp;
                holder.icon1.setLayoutParams(lp);
                ImageLoader.getInstance()
                        .displayImage(post.getPics().get(0).getUrl(), holder.icon1);
            } else if (i == 1) {
                holder.icon2.setVisibility(View.VISIBLE);
                android.view.ViewGroup.LayoutParams lp = holder.icon2.getLayoutParams();
                holder.icon2.setScaleType(ScaleType.CENTER_CROP);
                lp.width = commonLp;
                lp.height = commonLp;
                holder.icon2.setLayoutParams(lp);
                ImageLoader.getInstance()
                        .displayImage(post.getPics().get(1).getUrl(), holder.icon2);
            } else if (i == 2) {
                holder.icon3.setVisibility(View.VISIBLE);
                android.view.ViewGroup.LayoutParams lp = holder.icon3.getLayoutParams();
                holder.icon3.setScaleType(ScaleType.CENTER_CROP);
                lp.width = commonLp;
                lp.height = commonLp;
                holder.icon3.setLayoutParams(lp);
                ImageLoader.getInstance()
                        .displayImage(post.getPics().get(2).getUrl(), holder.icon3);
            }

        }



        //
        // if (post.getPics().size() == 1) {
        //
        // holder.icon1.setVisibility(View.VISIBLE);
        //
        // ImageLoader.getInstance().displayImage(post.getPics().get(0).getUrl(),
        // holder.icon1, defaultOptions);
        // } else if (post.getPics().size() == 2) {
        //
        //
        //
        // holder.icon1.setVisibility(View.VISIBLE);
        // // holder.icon1.setLayoutParams(new LayoutParams(l2, l2));
        // ImageLoader.getInstance().displayImage(post.getPics().get(0).getUrl(),
        // holder.icon1, defaultOptions);
        // holder.icon2.setVisibility(View.VISIBLE);
        // holder.icon2.setLayoutParams(new LayoutParams(l2, l2));
        // ImageLoader.getInstance().displayImage(post.getPics().get(1).getUrl(),
        // holder.icon2, defaultOptions);
        // } else if (post.getPics().size() == 3) {
        //
        //
        //
        // if (i == 0) {
        // holder.icon1.setVisibility(View.VISIBLE);
        // // holder.icon1.setLayoutParams(new LayoutParams(l3, l3));
        // ImageLoader.getInstance().displayImage(post.getPics().get(0).getUrl(),
        // holder.icon1, defaultOptions);
        // } else if (i == 1) {
        // holder.icon2.setVisibility(View.VISIBLE);
        // // holder.icon2.setLayoutParams(new LayoutParams(l3, l3));
        // ImageLoader.getInstance().displayImage(post.getPics().get(1).getUrl(),
        // holder.icon2, defaultOptions);
        // } else if (i == 2) {
        // holder.icon3.setVisibility(View.VISIBLE);
        // // holder.icon3.setLayoutParams(new LayoutParams(l3, l3));
        // ImageLoader.getInstance().displayImage(post.getPics().get(2).getUrl(),
        // holder.icon3, defaultOptions);
        // }
        //
        // }



        if (CommonParameter.isLoadImg) {
            ImageLoader.getInstance().displayImage(post.getUserface(), holder.usericon,
                    BitmapUtil.getPersonLoading());
        } else {
            holder.usericon.setImageResource(R.drawable.person_loading);
        }
        if (CommonParameter.isLoadImg) {
            ImageLoader.getInstance().displayImage(post.getUserface(), holder.post_icon,
                    BitmapUtil.getPersonLoading());
        } else {
            holder.post_icon.setImageResource(R.drawable.person_loading);
        }
        holder.username.setText(post.getAuthor());

        holder.title.setText(ParseMsgUtil.convetToHtml(CommonUtil.parseEmoji(post.getSubject()),
                mContext));

        // holder.look.setText(post.getViews() + "访问");
        // holder.care.setText(post.getRecommend_add());
        holder.message.setText("来自" + post.forumname);

        String time = CommonUtil.formatDate();
        String year = time.substring(0, 4);
        String hour = time.split(" ")[1].split(":")[0];
        String min = time.split(" ")[1].split(":")[1];

        String mTime = null;
        mTime = post.getAddtime();
        String mYear = mTime.substring(0, 4);
        String mMonth = mTime.split(" ")[0].split("-")[1];
        String mDay = mTime.split(" ")[0].split("-")[2];
        String mHour = mTime.split(" ")[1].split(":")[0];
        String mMin = mTime.split(" ")[1].split(":")[1];

        if (year.equals(mYear)) {// 说明是今年
            if (mTime.split(" ")[0].equals(time.split(" ")[0])) {// 说明是今天
                holder.time.setText(mHour + ":" + mMin);
            } else {
                holder.time.setText(mMonth + "-" + mDay);
            }
        } else {
            holder.time.setText(mTime.split(" ")[0]);
        }

        contentView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(mContext, PostDetailActivity.class);
                intent.putExtra(Constants.KEY_FROM_PAGE, 0);
                intent.putExtra(Constants.KEY_POST_TID, post.getTid());
                intent.putExtra(Constants.KEY_FROM_PAGE, 1);
                mContext.startActivity(intent);
            }
        });

        return contentView;
    }

    class ViewHolder {
        LinearLayout ll;
        CircleImageView usericon;
        CircleImageView post_icon;
        TextView username;
        TextView title;
        ImageView type;
        TextView look;
        TextView message;
        TextView time;
        ImageView icon1, icon2, icon3;
    }

}
